<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="userDialogFormRef" :model="state.userInfo" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="账户名称">
							<el-input v-model="state.userInfo.name" placeholder="请输入账户名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="手机号">
							<el-input v-model="state.userInfo.phone" placeholder="请输入手机号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="角色">
							<el-select v-model="state.userInfo.roleCodeList" placeholder="请选择角色" multiple class="w100">
								<el-option v-for="item in state.roleOption" :key="item.id" :label="item.roleName"
									:value="item.roleCode" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="邮箱">
							<el-input v-model="state.userInfo.email" placeholder="请输入" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="账户密码">
							<el-input v-model="state.userInfo.password" placeholder="请输入" type="password"
								clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemUserDialog">
import { reactive, ref, onMounted } from 'vue';
import { useUserApi } from '/@/api/user';
import { useRoleApi } from '/@/api/role';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const userDialogFormRef = ref();
const state: UserDialog = reactive({
	userInfo: {
		id: '',
		name: '', // 账户名称
		phone: '', // 手机号
		email: '', // 邮箱
		password: '', // 账户密码
		photo: '',
		roleCodeList: [],
	},
	roleOption: [],
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

const userApi = useUserApi();
const roleApi = useRoleApi();

// 清空用户数据
const clearUserInfo = () => {
	state.userInfo = {
		id: '',
		name: '', // 账户名称
		phone: '', // 手机号
		email: '', // 邮箱
		password: '', // 账户密码
		photo: '',
		roleCodeList: [],
	}
}

// 打开弹窗
async function openDialog(type: string, row?: any) {
	if (type === 'edit') {
		state.userInfo = row;
		state.dialog.title = '修改用户';
		state.dialog.submitTxt = '修 改';
	} else {
		clearUserInfo();
		state.dialog.title = '新增用户';
		state.dialog.submitTxt = '新 增';
	}
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
async function onSubmit() {
	await userApi.editUser(state.userInfo);
	closeDialog();
	emit('refresh');
	// if (state.dialog.type === 'add') { }
};

// 页面加载时
onMounted(() => {
	initRoleOption();
});

// 构建角色枚举值
async function initRoleOption() {
	const roleData = await roleApi.allRoleList();
	if (roleData.data.length <= 0)
		return Promise.resolve(true);
	const roleList: Role[] = roleData.data.rows;
	state.roleOption = roleList;
}

// 暴露变量
defineExpose({
	openDialog,
});
</script>
